package com.ten.shou.service.impl;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ten.common.core.entity.TbBottomArticle;
import com.ten.common.core.vo.Result;
import com.ten.shou.dao.ArticleDao;
import com.ten.shou.dao.BottomDao;
import com.ten.shou.service.BottomService;
import com.ten.shou.vo.ArticleVo;
import com.ten.shou.vo.BottomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author ：杨（1964958771@qq.com）
 * @date ：Created in 2022/12/9 14:43
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class BottomServiceImpl extends ServiceImpl<ArticleDao,ArticleVo> implements BottomService {
    @Resource
    private BottomDao bottomDao;
    @Resource
    private ArticleDao articleDao;
    @Override
    public Result<TbBottomArticle> selectAllBottom(Integer current, Integer pageSize, BottomVo bottomVo) {
        IPage<TbBottomArticle> page=new Page<>(current,pageSize);
        QueryWrapper<TbBottomArticle> wrapper=new QueryWrapper<>();
        if (StringUtils.hasText(bottomVo.getName())){
            wrapper.like("name",bottomVo.getName());
        }
        if (StringUtils.hasText(bottomVo.getIsDisable())){
             wrapper.eq("is_disable",bottomVo.getIsDisable());
        }
        List<TbBottomArticle> bottomArticles = bottomDao.selectList(wrapper);
        wrapper.eq("parent_id","0");
        IPage<TbBottomArticle> page1 = bottomDao.selectPage(page, wrapper);
        //对records中的信息进行层级的划分
        List<TbBottomArticle> records = page1.getRecords();
        for (TbBottomArticle record : records) {
            setTreeChildren(record,bottomArticles);
        }
        return new Result(2000,"查询成功",page1);
    }

    @Override
    public Result<TbBottomArticle> insertAllBottom(TbBottomArticle tbBottomArticle) {
        bottomDao.insert(tbBottomArticle);
        return new Result<>(2000,"添加成功");
    }

    @Override
    public Result<TbBottomArticle> deleteByIdBottom(TbBottomArticle tbBottomArticle) {
        bottomDao.deleteById(tbBottomArticle.getId());
        return new Result<>(2000,"删除成功");
    }

    @Override
    public Result<TbBottomArticle> updateStatusBottom(String id ,int isDisable) {
        UpdateWrapper<TbBottomArticle> wrapper=new UpdateWrapper<>();
        if (StringUtils.hasText(id)){
            wrapper.eq("id",id);
        }
        if (StringUtils.hasText("isDisable")){
            wrapper.set("is_disable",isDisable);
        }
        bottomDao.update(null,wrapper);

        return new Result(2000,"修改成功");
    }

    @Override
    public Result<TbBottomArticle> updateBottomTitle(TbBottomArticle tbBottomArticle) {
        bottomDao.updateById(tbBottomArticle);
        return new Result<>(2000,"修改成功");
    }

    @Override
    public Result getArticleByNavId(ArticleVo articleVo) {
        QueryWrapper<ArticleVo> wrapper = new QueryWrapper<>();
         wrapper.eq("nav_id",articleVo.getNavId());
        List<ArticleVo> list = articleDao.selectList(wrapper);
        if (ObjectUtil.isNotEmpty(list)){
            return new Result(2000,"查询成功",list.get(0));
        }
        return new Result(2000, "",articleVo);
    }

    @Override
    public Result updateArticleByNavId(ArticleVo articleVo) {
        if (ObjectUtil.isNotEmpty(articleVo.getId())) {
            int res = articleDao.updateById(articleVo);
            if (res > 0) {
                return new Result(2000, "提交成功");
            }
        } else {
            articleVo.setGmtCreate(LocalDateTime.now());
            articleVo.setIsDeleted(0);
            int res = articleDao.insert(articleVo);
            if (res > 0) {
                return new Result(2000, "提交成功");
            }
        }
        return new Result(5000, "提交失败");
    }

    @Override
    public Result getQianAllBottomArticle() {
        QueryWrapper<TbBottomArticle> wrapper=new QueryWrapper<>();
        wrapper.eq("is_disable","0");
        List<TbBottomArticle> tbBottomArticles = bottomDao.selectList(wrapper);
        List<TbBottomArticle> tbBottomArticles1 = fromEduSubjectListToTreeEduSubjectList(tbBottomArticles);
        return new Result(2000,"查询成功",tbBottomArticles1);
    }
@Transactional
    @Override
    public Result getAllQianArticle(String id) {
        ArticleVo allQianArticle = bottomDao.getAllQianArticle(id);
        return new Result(2000,"" ,allQianArticle);
    }


    //将获取到的菜单权限进行层级的分配
    private List<TbBottomArticle> fromEduSubjectListToTreeEduSubjectList(List<TbBottomArticle> tbBottomArticles) {
        List<TbBottomArticle> treeList = new ArrayList<>();
        for (TbBottomArticle tbBottomArticle : tbBottomArticles) {
                //为一级菜单
                if (tbBottomArticle.getParentId().equals("0")) {
                    //设置子菜单（其中包含二级菜单等下面不知层级数的菜单，采用递归调用）
                    //得到一级菜单包括一级菜单下的子菜单
                    TbBottomArticle childrenPermission = setTreeChildren(tbBottomArticle, tbBottomArticles);
                    treeList.add(childrenPermission);
                }
        }
        return treeList;
    }

    private TbBottomArticle setTreeChildren(TbBottomArticle tbBottomArticle, List<TbBottomArticle> tbBottomArticles) {
        List<TbBottomArticle> childrenList = new ArrayList<>();
        for (TbBottomArticle tbBottomArticle1 : tbBottomArticles) {
            if (Objects.equals(tbBottomArticle1.getParentId(), tbBottomArticle.getId())) {
                //继续调用设置子菜单
                TbBottomArticle treeChildren = setTreeChildren(tbBottomArticle1, tbBottomArticles);
                childrenList.add(treeChildren);
            }
        }
        //将封装好的数据设置到子菜单中
        tbBottomArticle.setChildren(childrenList);
        return tbBottomArticle;
    }

}
